package com.shizhanzhe.qdd.forward;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.base.BaseFragment;
import com.shizhanzhe.qdd.forward.activity.ClassManagerActivity;
import com.shizhanzhe.qdd.forward.activity.SearchActivity;
import com.shizhanzhe.qdd.forward.adapter.TabAdapter;
import com.shizhanzhe.qdd.forward.bean.Class;
import com.shizhanzhe.qdd.forward.bean.ShareType;
import com.shizhanzhe.qdd.url.Path;
import com.shizhanzhe.qdd.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.qdd.utils.SharedPreferencesUtils;
import com.shizhanzhe.qdd.widget.ErrorCallback;
import com.shizhanzhe.qdd.widget.tablayout.TabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by YJ on 2019/1/3.
 * 转发赚钱
 */
public class ForwardFragment extends BaseFragment {
    @BindView(R.id.table)
    TabLayout table;
    @BindView(R.id.vp)
    ViewPager vp;
    Unbinder unbinder;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.search_tv)
    TextView searchTv;
    private String[] ids = new String[]
            {"0", "1", "2", "3", "4", "5",
                    "6", "7", "8", "9", "10", "11", "12",
                    "13", "14", "15", "16", "17"};
    private int cposition = 0;


    @Override
    protected int getLayout() {
        return R.layout.fragment_forward;
    }

    private List<String> names ;

    @Override
    protected void getData() {
        names = new ArrayList<>();
        String json = (String) SharedPreferencesUtils.getParam(getActivity(), "my", "");
        if (!"".equals(json)) {

            Gson gson = new Gson();
            List<Class> list = gson.fromJson(json, new TypeToken<List<Class>>() {
            }.getType());
            List<Fragment> fragments = new ArrayList<>();
            for (Class bean : list
                    ) {
                if (bean.isShow()){
                    names.add(bean.getName());
                }

            }
            for (int i = 0; i < names.size(); i++) {
                fragments.add(ForWardItemFragment.newInstance(list.get(i).getId()));
            }
            TabAdapter adapter = new TabAdapter(getChildFragmentManager(), fragments, names);
            //给ViewPager设置适配器
            vp.setAdapter(adapter);
            //设置可以滑动
            table.setTabMode(TabLayout.MODE_SCROLLABLE);
            table.setTabGravity(TabLayout.GRAVITY_CENTER);
            table.setNeedSwitchAnimation(true);
            table.setIndicatorWidthWrapContent(true);
            //将TabLayout和ViewPager关联起来。
            table.setupWithViewPager(vp);
            table.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    cposition = tab.getPosition();
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });
            loadService.showSuccess();
        } else {
            OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.SHARETYPE(), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                @Override
                public void onsendJson(String json) {
                    if (!"1".equals(json) && !"2".equals(json)) {
                        Gson gson = new Gson();
                        try {
                            List<ShareType.DataBean> data = gson.fromJson(json, ShareType.class).getData();
                            names.add("每日推荐");
                            for (ShareType.DataBean bean : data
                                    ) {
                                names.add(bean.getCname());
                            }

                            List<Fragment> fragments = new ArrayList<>();
                            for (int i = 0; i < names.size(); i++) {
                                fragments.add(ForWardItemFragment.newInstance(ids[i]));
                            }
                            TabAdapter adapter = new TabAdapter(getChildFragmentManager(), fragments, names);
                            //给ViewPager设置适配器
                            vp.setAdapter(adapter);
                            //设置可以滑动
                            table.setTabMode(TabLayout.MODE_SCROLLABLE);
                            table.setTabGravity(TabLayout.GRAVITY_CENTER);
                            table.setNeedSwitchAnimation(true);
                            table.setIndicatorWidthWrapContent(true);
                            //将TabLayout和ViewPager关联起来。
                            table.setupWithViewPager(vp);
                            table.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                                @Override
                                public void onTabSelected(TabLayout.Tab tab) {
                                    cposition = tab.getPosition();
                                }

                                @Override
                                public void onTabUnselected(TabLayout.Tab tab) {

                                }

                                @Override
                                public void onTabReselected(TabLayout.Tab tab) {

                                }
                            });
                            loadService.showSuccess();
                        } catch (Exception e) {
                            loadService.showCallback(ErrorCallback.class);
                        }
                    } else {
                        loadService.showCallback(ErrorCallback.class);
                    }
                }

            });
        }

    }


    @OnClick({R.id.iv_more, R.id.search_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_more:
                startActivityForResult(new Intent(getActivity(), ClassManagerActivity.class), 0);
//                ForwardPopupWindow popupWindow = new ForwardPopupWindow(getActivity(), names);
//                popupWindow.showDown(view1, cposition);
//                popupWindow.setListener(new ForwardPopupWindow.ForwardListener() {
//                    @Override
//                    public void itemCheck(int itemid) {
//                        table.getTabAt(itemid).select();
//                    }
//                });
                break;
            case R.id.search_tv:
                startActivity(new Intent(getContext(), SearchActivity.class));
                break;
            default:
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            Gson gson = new Gson();
            String json = data.getStringExtra("json");
            if (!"".equals(json)) {
                List<Class> list = gson.fromJson(json, new TypeToken<List<Class>>() {
                }.getType());
                List<Fragment> fragments = new ArrayList<>();
                names=new ArrayList<>();
                for (Class bean : list
                        ) {
                    if (bean.isShow()){
                        names.add(bean.getName());
                    }
                }
                for (int i = 0; i < names.size(); i++) {
                    fragments.add(ForWardItemFragment.newInstance(list.get(i).getId()));
                }
                TabAdapter adapter = new TabAdapter(getChildFragmentManager(), fragments, names);
                //给ViewPager设置适配器
                vp.setAdapter(adapter);

                //设置可以滑动
                table.setTabMode(TabLayout.MODE_SCROLLABLE);
                table.setTabGravity(TabLayout.GRAVITY_CENTER);
                table.setNeedSwitchAnimation(true);
                table.setIndicatorWidthWrapContent(true);
                //将TabLayout和ViewPager关联起来。
                table.setupWithViewPager(vp);
                table.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        cposition = tab.getPosition();
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });
            }
        }
    }

}
